package com.fx.algorithm.first;

/**
 * 加一
 * @author fx
 */
public class Leetcode66 {

    public int[] plusOne(int[] digits) {
        for(int i=digits.length-1;i>=0;i--){
            digits[i]++;
            digits[i] = digits[i]%10;
            if(digits[i]!=0) return digits;
        }
        digits = new int[digits.length+1];
        digits[0]=1;
        return digits;
    }

    public int[] plusOne2(int[] digits) {
        int length = digits.length;
        int lastNum = digits[length - 1];
        if (lastNum < 9) {
            digits[length - 1] = lastNum+1;
            return digits;
        }
        for (int i = length-1; i >=0; i--) {
            lastNum = digits[i];
            if (lastNum < 9) {
                digits[i] = lastNum+1;
                return digits;
            }else{
                digits[i] = 0;
            }
        }
        if (digits[0] == 0) {
            digits = new int[digits.length+1];
            digits[0]=1;
            return digits;
        }
        return digits;
    }
}
